package com.group4.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.group4.edu.entity.Chapter;
import com.group4.edu.entity.Video;
import com.group4.edu.mapper.ChapterMapper;
import com.group4.edu.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group4.edu.service.VideoService;
import com.study.exceptions.ChapterException;
import com.study.vo.ChapterVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author cloud
 * @since 2022-03-10
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private ChapterMapper chapterMapper;

    @Override
    public List<ChapterVO> selectChapterAndVideoByCourseId(String courseId) {
        return chapterMapper.selectChapterAndVideoByCourseId(courseId);
    }

    @Autowired
    private VideoService videoService;
    @Override
    public void deleteChapterById(String id) {
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",id);
        int count = videoService.count(queryWrapper);
        if (count > 0) {
            throw new ChapterException("请先删除小节");
        }

        baseMapper.deleteById(id);

    }
}
